<template>
	<main-content
		:tableHeight.sync="tableHeight"
		:otherDependenComponents="otherDependenComponents"
	>
		<template slot="query">
			<el-form
				ref="queryForm"
				:inline="true"
				:model="queryData"
				class="flex-warp-container"
			>
				<el-form-item
					:label="$t('common.choose_merchant') + ':'"
					prop="merchantId"
					class="flex-row-container"
					label-width="77px"
				>
					<merchant-select
						v-model="queryData.merchantId"
						:placeholder="$t('common.select_all')"
					></merchant-select>
				</el-form-item>
				<el-form-item
					:label="$t('operation.notice_config.message_title1') + ':'"
					class="flex-row-container"
					label-width="77px"
				>
					<el-input
						v-model="queryData.title"
						clearable
						:maxlength="100"
						v-input="{ name: 'symbols' }"
						size="medium"
						:placeholder="$t('common.please_enter')"
					></el-input>
				</el-form-item>
				<el-form-item
					:label="$t('operation.send_obj') + ':'"
					class="flex-row-container"
					label-width="77px"
				>
					<el-select
						v-model="queryData.sendObj"
						clearable
						:placeholder="$t('common.select_all')"
						:popper-append-to-body="false"
					>
						<el-option
							:label="$t('operation.member')"
							value="1"
						></el-option>
						<el-option
							:label="$t('operation.device')"
							value="2"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item
					:label="$t('common.createBy') + ':'"
					class="flex-row-container"
					label-width="61px"
				>
					<el-input
						v-model="queryData.createdBy"
						clearable
						size="medium"
						v-input="{ name: 'symbols' }"
						:placeholder="$t('common.please_enter')"
					></el-input>
				</el-form-item>
				<el-form-item>
					<el-button
						type="primary"
						icon="el-icon-search"
						:disabled="loading"
						size="medium"
						@click="search"
					>
						{{ $t('common.search') }}
					</el-button>
					<el-button
						icon="el-icon-refresh-left"
						:disabled="loading"
						size="medium"
						@click="reset"
					>
						{{ $t('common.reset') }}
					</el-button>
					<el-button
						v-if="hasPermission('502031')"
						type="warning"
						icon="el-icon-folder"
						:disabled="loading"
						size="medium"
						@click="Add"
					>
						{{ $t('common.add') }}
					</el-button>
				</el-form-item>
			</el-form>
		</template>
		<template slot="content">
			<el-table
				v-loading="loading"
				element-loading-spinner="el-icon-loading"
				border
				size="mini"
				class="small-size-table"
				:data="list"
				highlight-current-row
				:header-cell-style="getRowClass"
				:max-height="tableHeight"
			>
				<el-table-column
					prop="title"
					align="center"
					:label="$t('operation.notice_config.message_title1')"
					width="200px"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span>
							{{ scope.row.title || '-' }}
						</span>
						<!--<p v-if="scope.row.titleEn">
									{{ $t('common.en') }}：{{
										scope.row.titleEn || '-'
									}}
								</p>
								<p v-if="scope.row.titleTh">
									{{ $t('common.th') }}：{{
										scope.row.titleTh || '-'
									}}
								</p>
								<p v-if="scope.row.titleVi">
									{{ $t('common.vn') }}：{{
										scope.row.titleVi || '-'
									}}
								</p>-->
					</template>
				</el-table-column>
				<el-table-column
					align="center"
					:label="$t('operation.notice_config.notice_content')"
					width="200px"
					prop="content"
				>
					<template slot-scope="scope">
						<el-tooltip
							v-if="scope.row.content.length > 12"
							class="item"
							effect="dark"
							placement="top"
						>
							<div class="tooltip-content" slot="content">
								{{ scope.row.content || '-' }}
							</div>
							<div class="tooltip-trigger">
								{{ scope.row.content }}
							</div>
						</el-tooltip>
						<span v-else>
							{{ scope.row.content || '-' }}
						</span>
						<!--<p v-if="scope.row.contentEn">
									{{ $t('common.en') }}：{{
										scope.row.contentEn || '-'
									}}
								</p>
								<p v-if="scope.row.contentTh">
									{{ $t('common.th') }}：{{
										scope.row.contentTh || '-'
									}}
								</p>
								<p v-if="scope.row.contentVi">
									{{ $t('common.vn') }}：{{
										scope.row.contentVi || '-'
									}}
								</p>-->
					</template>
				</el-table-column>
				<el-table-column
					prop="sendObj"
					align="center"
					:label="$t('operation.send_obj')"
					show-overflow-tooltip
				>
					<template v-if="scope.row.sendObj" slot-scope="scope">
						<span>
							{{
								scope.row.sendObj === 1
									? $t('operation.member')
									: $t('operation.device')
							}}
						</span>
					</template>
					<span v-else>-</span>
				</el-table-column>
				<el-table-column
					align="center"
					:label="$t('operation.send_obj_detail')"
					prop="sendObj"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span
							v-if="
								scope.row.sendObj === 2 &&
									scope.row.deviceType &&
									scope.row.deviceType.length === 0
							"
						>
							{{ joinDeviceTypeArr() }}
						</span>
						<span
							v-else-if="
								scope.row.sendObj === 2 &&
									scope.row.deviceType &&
									scope.row.deviceType.length
							"
						>
							{{ filterTerminal(scope.row.deviceType) }}
						</span>
						<div
							v-if="
								scope.row.sendObj === 1 &&
									scope.row.userType === 0
							"
						>
							{{ $t('common.all') }}
						</div>
						<div
							v-else-if="
								scope.row.sendObj === 1 &&
									scope.row.userType === 1
							"
						>
							{{
								scope.row.objDetail
									.split(',')
									.splice(0, 3)
									.join(',')
							}}
							<div
								v-if="scope.row.objDetail.split(',').length > 3"
								class="decoration"
								@click="lookGame(scope.row)"
							>
								{{ $t('operation.show_more') }}
							</div>
						</div>
					</template>
				</el-table-column>
				<el-table-column
					prop="createdBy"
					align="center"
					:label="$t('common.createBy')"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span>{{ scope.row.createdBy || '-' }}</span>
					</template>
				</el-table-column>
				<el-table-column
					prop="createdAt"
					align="center"
					:label="$t('common.createAt')"
					show-overflow-tooltip
				>
					<template slot-scope="scope">
						<span>{{ scope.row.createdAt || '-' }}</span>
					</template>
				</el-table-column>
				<el-table-column
					prop="operating"
					align="center"
					:label="$t('common.actions')"
				>
					<template slot-scope="scope">
						<el-button
							v-if="hasPermission('502033')"
							:disabled="scope.row.retract === 1"
							type="primary"
							size="medium"
							@click="withdraw(scope.row)"
						>
							{{ $t('common.withdraw') }}
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<el-pagination
				:current-page.sync="pageNum"
				class="pageValue"
				background
				layout="total, sizes,prev, pager, next, jumper"
				:page-size="pageSize"
				:page-sizes="pageSizes"
				:total="total"
				:pager-count="9"
				@current-change="handleCurrentChange"
				@size-change="handleSizeChange"
			></el-pagination>
		</template>
		<el-dialog
			:title="$t('common.add')"
			:visible.sync="dialogFormVisible"
			:destroy-on-close="true"
			width="645px"
			center
			@close="clear"
		>
			<el-form
				ref="formSub"
				:inline="true"
				:model="dialogForm"
				:rules="dataRule"
				label-width="83px"
			>
				<el-form-item
					:label="$t('common.choose_merchant') + ':'"
					class="tagheight"
					prop="merchantId"
				>
					<merchant-select
						v-model="dialogForm.merchantId"
						:placeholder="$t('common.please_choose')"
						:merchantInfo.sync="merchantInfoA"
						@change="handleMerchantIdChange"
					></merchant-select>
				</el-form-item>
				<h4 style="text-align:center">
					{{ $t('operation.notice_config.message_title1') }}
				</h4>
				<el-form-item
					:label="$t('common.zh') + ':'"
					class="tagheight"
					prop="title"
				>
					<el-input
						v-model="dialogForm.title"
						:placeholder="$t('common.please_enter')"
						:maxlength="100"
						show-word-limit
						clearable
						v-input="{ name: 'symbols' }"
						autocomplete="off"
					></el-input>
				</el-form-item>
				<el-divider></el-divider>
				<h4 style="text-align:center">
					{{ $t('operation.notice_config.notice_content') }}
				</h4>
				<el-form-item
					:label="$t('common.zh') + ':'"
					class="tagheight"
					prop="content"
				>
					<el-input
						v-model="dialogForm.content"
						:placeholder="$t('common.please_enter')"
						:maxlength="2000"
						clearable
						type="textarea"
						show-word-limit
					></el-input>
				</el-form-item>
				<el-divider></el-divider>
				<el-form-item
					:label="$t('operation.send_obj') + ':'"
					class="tagheight"
					prop="sendObj"
				>
					<el-select
						v-model="dialogForm.sendObj"
						:placeholder="$t('common.select_all')"
						:popper-append-to-body="false"
					>
						<el-option
							v-for="item in sendObjTypes"
							:key="item.code"
							:label="item.description"
							:value="item.code"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item
					v-show="dialogForm.sendObj === '1'"
					:label="$t('common.member_type') + ':'"
					class="tagheight"
					:rules="[
						{
							required: true
						}
					]"
				>
					<el-select
						v-model="dialogForm.userType"
						:placeholder="$t('common.select_all')"
						:popper-append-to-body="false"
					>
						<el-option
							v-for="item in userTypes"
							:key="item.code"
							:label="item.description"
							:value="item.code"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item
					v-show="dialogForm.sendObj === '2'"
					:label="$t('operation.send_device') + ':'"
					class="tagheight"
					prop="deviceType"
				>
					<el-select
						v-model="dialogForm.deviceType"
						:placeholder="$t('common.please_choose')"
						multiple
						clearable
						:popper-append-to-body="false"
					>
						<el-option
							v-for="item in loginDeviceType"
							:key="item.code"
							:label="item.description"
							:value="item.code"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item
					v-if="
						dialogForm.sendObj === '1' &&
							dialogForm.userType === '1'
					"
					:label="$t('common.member_number') + ':'"
					class="tagheight"
					prop="objDetail"
					:rules="[
						{
							required: true,
							message: memberValidErrorMsg,
							trigger: 'blur'
						}
					]"
				>
					<el-input
						v-model="dialogForm.objDetail"
						:placeholder="memberValidErrorMsg"
						clearable
						show-word-limit
					></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogFormVisible = false">
					{{ $t('common.cancel') }}
				</el-button>
				<el-button
					type="primary"
					:loading="loading"
					@click="subAddOrEidt"
				>
					{{ $t('common.save') }}
				</el-button>
			</div>
		</el-dialog>
		<el-dialog
			:title="$t('common.check')"
			:visible.sync="lookVisible"
			:destroy-on-close="true"
			class="rempadding"
		>
			<el-table
				v-loading="loading2"
				size="mini"
				border
				class="small-size-table"
				:data="userList"
				style="width: 100%; margin-bottom: 15px"
				:header-cell-style="getRowClass"
			>
				<el-table-column
					type="index"
					align="center"
					:label="$t('common.serial_number')"
					width="100"
				>
					<template slot-scope="scope">
						<span>{{ _getSerialNum(scope.$index) }}</span>
					</template>
				</el-table-column>
				<el-table-column
					prop="username"
					align="center"
					:label="$t('operation.send_member_number')"
				>
					<template slot-scope="scope">
						{{ scope.row.username }}
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<el-pagination
				:current-page.sync="pageD"
				background
				class="fenye"
				layout="total, sizes,prev, pager, next, jumper"
				:page-size="sizeD"
				:page-sizes="pageSizes"
				:total="summary"
				:pager-count="9"
				@current-change="handleCurrentChangeDialog"
				@size-change="handleSizeChangeDialog"
			></el-pagination>
		</el-dialog>
	</main-content>
</template>

<script>
import { routerNames } from '@/utils/consts'
import list from '@/mixins/list'
import { mapGetters } from 'vuex'
import MainContent from '@/components/MainContent/index.vue'
export default {
	name: routerNames.notice,
	components: { MainContent },
	mixins: [list],
	data() {
		return {
			memberValidErrorMsg: this.$t('operation.input_tips1'),
			loading2: false,
			tableHeight: 480,
			otherDependenComponents: [],
			queryData: {
				title: undefined,
				merchantId: undefined,
				sendObj: undefined,
				createdBy: undefined
			},
			dialogFormVisible: false,
			dialogForm: {
				merchantId: undefined,
				sendObj: '1',
				deviceType: [],
				userType: '0',
				contentVi: '',
				contentTh: '',
				contentEn: '',
				content: '',
				objDetail: '',
				titleVi: '',
				titleTh: '',
				titleEn: '',
				title: ''
			},
			lookVisible: false,
			recordId: '',
			userList: [],
			pageD: 1,
			sizeD: 10,
			summary: 0,
			merchantInfoA: {
				languages: 'zh_CN'
			}
		}
	},
	computed: {
		...mapGetters(['merchantInfo']),
		blackStatusType() {
			return this.globalDics.blackStatusType || []
		},
		loginDeviceType() {
			return this.globalDics.loginDeviceType || []
		},
		userTypes() {
			return [
				{
					code: '1',
					description: this.$t('operation.specific_member')
				},
				{
					code: '0',
					description: this.$t('operation.all_member')
				}
			]
		},
		sendObjTypes() {
			// return this.globalDics.loginDeviceType || []
			return [
				{ code: '1', description: this.$t('operation.member') },
				{ code: '2', description: this.$t('operation.device') }
			]
		},
		dataRule() {
			return {
				deviceType: [
					{
						required: true,
						message: this.$t('operation.input_tips2'),
						trigger: 'change'
					}
				],
				title: [
					{
						required: true,
						message: this.$t('operation.notice_config.input_tips5'),
						trigger: 'blur'
					},
					{
						min: 1,
						max: 100,
						message: this.$t('operation.limit100'),
						trigger: 'blur'
					}
				],
				titleEn: [
					{
						required: true,
						message: this.$t('operation.notice_config.input_tips5'),
						trigger: 'blur'
					},
					{
						min: 1,
						max: 100,
						message: this.$t('operation.limit100'),
						trigger: 'blur'
					}
				],
				titleTh: [
					{
						required: true,
						message: this.$t('operation.notice_config.input_tips5'),
						trigger: 'blur'
					},
					{
						min: 1,
						max: 100,
						message: this.$t('operation.limit100'),
						trigger: 'blur'
					}
				],
				titleVi: [
					{
						required: true,
						message: this.$t('operation.notice_config.input_tips5'),
						trigger: 'blur'
					},
					{
						min: 1,
						max: 100,
						message: this.$t('operation.limit100'),
						trigger: 'blur'
					}
				],
				content: [
					{
						required: true,
						message: this.$t('operation.notice_config.input_tips6'),
						trigger: 'blur'
					},
					{
						min: 1,
						max: 2000,
						message: this.$t('operation.limit2000'),
						trigger: 'blur'
					}
				],
				contentEn: [
					{
						required: true,
						message: this.$t('operation.notice_config.input_tips6'),
						trigger: 'blur'
					},
					{
						min: 1,
						max: 2000,
						message: this.$t('operation.limit2000'),
						trigger: 'blur'
					}
				],
				contentTh: [
					{
						required: true,
						message: this.$t('operation.notice_config.input_tips6'),
						trigger: 'blur'
					},
					{
						min: 1,
						max: 2000,
						message: this.$t('operation.limit2000'),
						trigger: 'blur'
					}
				],
				contentVi: [
					{
						required: true,
						message: this.$t('operation.notice_config.input_tips6'),
						trigger: 'blur'
					},
					{
						min: 1,
						max: 2000,
						message: this.$t('operation.limit2000'),
						trigger: 'blur'
					}
				],
				sendObj: [
					{
						required: true,
						message: this.$t('common.please_enter'),
						trigger: 'blur'
					}
				],
				merchantId: [
					{
						required: true,
						message: this.$t('operation.please_choose_merchant'),
						trigger: ['blur', 'change']
					}
				]
			}
		}
	},
	watch: {
		'dialogForm.userType'() {
			this.dialogForm.objDetail = undefined
		},
		'dialogForm.sendObj'(val) {
			if (val === '1') {
				this.dialogForm.deviceType = []
				this.dialogForm.userType = '0'
			} else {
				this.dialogForm.deviceType = this.loginDeviceType.map((i) =>
					String(i.code)
				)
				this.dialogForm.userType = '0'
			}
		}
	},
	mounted() {
		if (!this.$store.getters.isZk) {
			this.queryData.merchantId = this.$store.getters.merchantInfo.id
		}
		this.loadData()
	},
	methods: {
		// 改变列表条数
		handleSizeChange(value) {
			this.pageNum = 1
			this.pageSize = value
			this.loadData()
		},
		handleCurrentChange(value) {
			this.pageNum = value
			this.loadData()
		},
		search() {
			this.pageNum = 1
			this.loadData()
		},
		handleMerchantIdChange() {
			this.dialogForm.contentVi = ''
			this.dialogForm.contentTh = ''
			this.dialogForm.contentEn = ''
			this.dialogForm.content = ''
			this.dialogForm.titleVi = ''
			this.dialogForm.titleTh = ''
			this.dialogForm.titleEn = ''
			this.dialogForm.title = ''
			this.$refs.formSub.clearValidate([
				'contentVi',
				'contentTh',
				'contentEn',
				'content',
				'titleVi',
				'titleTh',
				'titleEn',
				'title'
			])
		},
		filterLanguages(lang) {
			return this.merchantInfoA?.languages
				? this.merchantInfoA?.languages?.indexOf(lang) !== -1
				: false
		},
		loadData() {
			const params = {
				noticeType: 2,
				...this.getParams(this.queryData)
			}
			for (const k in params) {
				if (params[k] === '') {
					delete params[k]
				}
			}
			this.list = []
			this.loading = true
			this.$api
				.getOperateObConfigNoticeSelectAll(params)
				.then((res) => {
					const { record, totalRecord } = res.data || {}
					this.list = record || []
					this.total = totalRecord || 0

					this.loading = false
				})
				.catch(() => {
					this.loading = false
				})
		},
		reset() {
			this.queryData = {
				title: undefined,
				merchantId: undefined,
				sendObj: undefined,
				createdBy: undefined
			}
			if (!this.$store.getters.isZk) {
				this.queryData.merchantId = this.$store.getters.merchantInfo.id
			}
			this.pageNum = 1
			this.loadData()
		},
		Add() {
			this.dialogForm.sendObj = '1'
			this.dialogForm.deviceType = ['1']
			this.dialogForm.userType = '0'
			this.dialogForm.contentVi = ''
			this.dialogForm.contentTh = ''
			this.dialogForm.contentEn = ''
			this.dialogForm.content = ''
			this.dialogForm.titleVi = ''
			this.dialogForm.titleTh = ''
			this.dialogForm.titleEn = ''
			this.dialogForm.title = ''
			this.dialogForm.objDetail = ''
			if (!this.$store.getters.isZk) {
				this.dialogForm.merchantId = this.$store.getters.merchantInfo.id
			} else {
				this.dialogForm.merchantId = undefined
			}
			this.dialogFormVisible = true
			this.$nextTick(() => {
				this.$refs.formSub.clearValidate()
			})
		},
		subAddOrEidt() {
			const params = {
				noticeType: 2,
				...this.dialogForm
			}
			for (const k in params) {
				if (
					params[k] === '' ||
					(Array.isArray(params[k]) && !params[k].length)
				) {
					delete params[k]
				}
			}
			if (params.deviceType) {
				params.deviceType = params.deviceType.join(',')
			}
			this.$refs.formSub.validate((valid) => {
				if (valid) {
					this.loading = true
					if (this.dialogForm.sendObj === '1') {
						delete params.deviceType
					} else {
						delete params.userType
					}
					this.$api
						.getOperateConfigNoticeSave(params)
						.then((res) => {
							this.loading = false
							if (res?.code === 200) {
								this.$message.success(
									this.$t('common.action_success')
								)
								this.dialogFormVisible = false
								this.loadData()
							}
						})
						.catch((e) => {
							this.loading = false
						})
				}
			})
		},
		withdraw(row) {
			this.$confirm(
				`<strong>${this.$t(
					'operation.switch_status_tips4'
				)}?</strong></br><span style='font-size:12px;color:#c1c1c1'>${this.$t(
					'operation.warning_tips'
				)}</span>`,
				this.$t('common.success_tip'),
				{
					dangerouslyUseHTMLString: true,
					confirmButtonText: this.$t('common.confirm'),
					cancelButtonText: this.$t('common.cancel'),
					type: 'warning'
				}
			)
				.then(() => {
					const params = {}
					params.retract = 1
					params.noticeType = 2
					params.messageId = row.id
					this.$api
						.getOperateConfigNoticeRetract(params)
						.then((res) => {
							if (res?.code === 200) {
								this.$message.success(
									this.$t('operation.withdraw_success')
								)
								this.loadData()
							}
						})
				})
				.catch(() => {})
		},
		clear() {
			this.$refs.formSub.resetFields()
		},
		// 弹框标签添加人数
		getOperateConfigNoticeSelectDetail() {
			this.loading2 = true
			const params = {}
			params.noticeId = this.recordId
			params.configType = 1
			params.pageNum = this.pageD
			params.pageSize = this.sizeD
			this.$api.getBwConfigNoticeDetails(params).then((res) => {
				if (res?.code === 200) {
					this.userList = res.data?.record || []
					this.summary = res.data?.totalRecord || 0
					this.lookVisible = true
				}
				this.loading2 = false
			})
		},
		lookGame(val) {
			this.pageD = 1
			this.sizeD = 10
			this.userList = []
			this.recordId = val.id
			this.lookVisible = true
			this.getOperateConfigNoticeSelectDetail()
		},
		handleCurrentChangeDialog(val) {
			this.pageD = val
			this.getOperateConfigNoticeSelectDetail()
		},
		handleSizeChangeDialog(val) {
			this.sizeD = val
			this.getOperateConfigNoticeSelectDetail()
		},

		filterTerminal(val) {
			const arr = val ? val.split(',') : []
			if (arr.length) {
				return arr
					.map((item) => this.typeFilter(item, 'clientType'))
					.join(',')
			}
			return '-'
		},
		joinDeviceTypeArr() {
			const arr = []
			this.loginDeviceType.forEach((item) => {
				arr.push(item.description)
			})
			return arr.length ? arr.join(',') : '-'
		},
		_getSerialNum(index) {
			return (this.pageD - 1) * this.sizeD + index + 1
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
	text-align: center;
	color: #909399;
	font-weight: 700;
	border: 1px solid #dddddd;
}
::v-deep .tagheight {
	.el-form-item__content,
	.el-select {
		width: 480px;
	}
}
.tooltip-content {
	width: 250px;
}
.tooltip-trigger {
	white-space: nowrap;
	text-overflow: ellipsis;
	overflow: hidden;
}
.decoration {
	// text-decoration: underline;
	cursor: pointer;
	color: #4b7902;
}
.fenye {
	text-align: center;
}

.vi_VN {
	.tagheight ::v-deep .el-form-item__content,
	.tagheight .el-select {
		width: 520px;
	}
}
</style>
<style>
.el-tooltip__popper {
	max-width: 300px !important;
}
</style>
